<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\UserIdentity;
use App\Models\FirmProfession;
use App\Models\Message;
use App\Models\User;
use App\Services\TtsService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class UserIdentityController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserIdentity(['user:id,nickname,avatar','profession:id,name','auth:user_id,name']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('avatar','形象照')->image('',80,80);
            $grid->column('auth.name','真实姓名');
            $grid->column('identity')
                 ->using([1 => '讲师',2 => '顾问',3 => '名人']);
            $grid->column('profession.name','入驻行业');
            $grid->column('audit_status')
                ->using([1 => '待审核',2 =>'已通过',3 => '已拒绝'])
                ->label([1 => 'danger',2 => 'success',3 => 'warning']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                    $filter->panel();
                    $filter->equal('user.nickname','用户昵称')
                            ->width(3);
                    $filter->equal('auth.name','真实姓名')
                           ->width(3);
                    $filter->equal('identity')
                           ->select([1 => '讲师',2 => '顾问',3 => '名人'])
                           ->width(3);

                    $filter->equal('audit_status')
                           ->select([1 => '待审核',2 =>'已通过',3 => '已拒绝'])
                           ->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserIdentity(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('identity');
            $show->field('firm_profession_id');
            $show->field('certificate_photo');
            $show->field('intro');
            $show->field('audit_status');
            $show->field('reject_reason');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserIdentity(), function (Form $form) {
            $form->display('id');
            $form->selectTable('user_id','用户')
                ->from(UserTable::make())
                ->model(User::class,'id','nickname')
                ->required();

            $form->image('avatar','形象照')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($value){
                    return (string)$value;
                });

            $form->radio('identity')
                 ->options([1 => '讲师',2 => '顾问',3 => '名人'])
                 ->required();
            $form->select('firm_profession_id')
                 ->options(FirmProfession::getOption())
                 ->required();
            $form->image('certificate_photos','凭证')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($value){
                    return (string)$value;
                });
            $form->editor('intro')
                ->saving(function ($value){
                    return (string)$value;
                });

            $form->radio('is_consult','咨询')
                 ->options([1 => '开启',2 => '关闭'])
                 ->default(2)
                 ->required();

            $form->radio('audit_status')
                ->options([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->when(3,function (Form $form){
                    $form->textarea('reject_reason')
                        ->saving(function ($value){
                            return (string)$value;
                        })->rules('max:255',['max' => '最多500字']);
                })->required();


            $form->hidden('task_id');
            $form->hidden('audio_url');
            $form->saving(function (Form $form){
                if ($form->input('audit_status')==2){
                    switch ($form->input('identity')){
                        case 1:
                            $identity = '讲师';
                            break;
                        case 2:
                            $identity = '顾问';
                             break;
                        case 3:
                            $identity = '名人';
                            break;
                    }
                    $auth = DB::table('users')->where('id',$form->input('user_id'))->value('auth');

                    if ($auth==1){
                        $name = DB::table('user_auths')->where('user_id',$form->input('user_id'))->value('name');
                    }else{
                        $name = DB::table('firms')->where('user_id',$form->input('user_id'))->value('corporate_name');
                    }


                    if ($form->isCreating()){
                        $tts = new TtsService();
                        $task_id = $tts->ttsCreate(strip_tags($form->input('intro')));
                        $form->input('task_id',$task_id);
                    }

                    if ($form->isEditing() && $form->input('intro') != $form->model()->intro){
                        $tts = new TtsService();
                        $task_id = $tts->ttsCreate(strip_tags($form->input('intro')));


                        $form->input('task_id',$task_id);
                        $form->input('audio_url','');
                    }




                    Message::query()->create([
                        'type' => 6,
                        'content' => $name . '申请了'.$identity.'入驻'
                    ]);
                }

                $form->input('task_id',(string)$form->input('task_id'));
                $form->input('audio_url',(string)$form->input('audio_url'));
            });

            $form->saved(function (Form $form){

                $identity = '';
                switch ($form->input('identity')){
                    case 1:
                        $identity = '讲师';
                        break;
                    case 2:
                        $identity = '顾问';
                        break;
                    case 3:
                        $identity = '名人';
                        break;
                }

                if ($form->input('user_id') && $form->input('audit_status')==2){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '恭喜您申请的'.$identity.'已通过审核'
                    ]));
                }

                if ($form->input('user_id') && $form->input('audit_status')==3){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '您申请的'.$identity.'未通过审核'.$form->input('reject_reason')
                    ]));
                }

            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
